package net.zjitc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.zjitc.common.R;
import net.zjitc.dto.SetMealDto;
import net.zjitc.entity.Category;
import net.zjitc.entity.SetMeal;
import net.zjitc.entity.SetMealDish;
import net.zjitc.service.CategoryService;
import net.zjitc.service.SetMealDishService;
import net.zjitc.service.SetMealService;
import net.zjitc.service.impl.SetMealServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/setmeal")
@RestController
@Slf4j
public class SetMealController {
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetMealDishService setMealDishService;
    @PostMapping
    public R<String> save(@RequestBody SetMealDto setMealDto){
        log.info("保存套餐"+setMealDto);
        setMealService.saveWithDish(setMealDto);
        return R.success("保存套餐成功");
    }
    @GetMapping("/page")
    public R<Page<SetMealDto>> getPage(Integer page,Integer pageSize,String name){
        log.info("查询的页面数据为："+page,pageSize,name);
        Page<SetMeal> setMealPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<SetMeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Strings.isNotEmpty(name),SetMeal::getName,name);
        setMealService.page(setMealPage,wrapper);

        Page<SetMealDto> setMealDtoPage = new Page<>();
        BeanUtils.copyProperties(setMealPage,setMealDtoPage,"records");
        List<SetMeal> setMealList = setMealPage.getRecords();
        List<SetMealDto> setMealDtoList=setMealList.stream().map((item)->{
            SetMealDto setMealDto = new SetMealDto();
            BeanUtils.copyProperties(item,setMealDto);
            Category category = categoryService.getById(item.getCategoryId());
            setMealDto.setCategoryName(category.getName());
            return setMealDto;
        }).collect(Collectors.toList());
        setMealDtoPage.setRecords(setMealDtoList);
        return R.success(setMealDtoPage);
    }
    @DeleteMapping
    private R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        setMealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable("status") Integer status, @RequestParam("ids") List<Long> ids) {
        log.info("status:" + status + "/ids" + ids);
        for (Long id : ids) {
            SetMeal setmeal = new SetMeal();
            setmeal.setStatus(status);
            setmeal.setId(id);
            setMealService.updateById(setmeal);
        }
        return R.success("修改状态成功");
    }
    @GetMapping("/list")
    public R<List<SetMeal>> list(SetMeal setmeal) {
        LambdaQueryWrapper<SetMeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, SetMeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, SetMeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(SetMeal::getUpdateTime);

        List<SetMeal> list = setMealService.list(queryWrapper);

        return R.success(list);
    }
    @GetMapping("/{id}")
    public R<SetMealDto> findById(@PathVariable Long id) {
        log.info("填充表单:" + id);
        SetMeal setmeal = setMealService.getById(id);
        LambdaQueryWrapper<SetMealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetMealDish::getSetmealId, id);
        List<SetMealDish> list = setMealDishService.list(setmealDishLambdaQueryWrapper);
        SetMealDto setmealDto = new SetMealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }
    @PutMapping
    public R<String> update(@RequestBody SetMealDto setmealDto) {
        log.info("更新setmealDto" + setmealDto);
        //第一步是更新套餐updateById

        //第二部是更新套餐-菜品关系表 先删除，再添加 这个逻辑和口味-菜品关系表是一样的
        setMealService.updateWithDish(setmealDto);
        return R.success("更新成功");
    }
}
